package com.example.hotelsystem.controller.statistics;

import com.example.hotelsystem.entity.Profit;
import com.example.hotelsystem.mapper.HotelorderMapper;
import com.example.hotelsystem.mapper.UserMapper;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.ProfitService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
public class Statistics {

    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);


    @Autowired
    UserMapper userMapper;

    @Autowired
    HotelorderMapper hotelorderMapper;

    @Autowired
    ProfitService profitService;

    @Async
    @Scheduled(cron = "0 59 23 * * ?")
    public void ProfitStatistics(){
        Date today = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Long userAmount = userMapper.NewAddUser(sdf.format(today));
        Double dayProfit = hotelorderMapper.DayProfit(sdf.format(today));
        Profit profit = new Profit();
        profit.setDay(today);
        profit.setPriceAmount(dayProfit!=null?dayProfit:0);
        profit.setUserAmount(userAmount!=null?userAmount:0);
        boolean isAdd = profitService.save(profit);
        if(isAdd){
            logger.info("于"+today+"完成统计操作");
        }
        logger.error(today+":统计发生错误");
    }

    @Async
    @Scheduled(cron = "0 59 23 * * ?")
    public void checkOrder(){
        try {
            Date date = new Date();
        }catch (Exception e){
            logger.error(e.getMessage());
        }
    }
}
